package kugou

import (
	"api-go/cache"
	"api-go/serializer"
	"api-go/util"
	"encoding/json"
	"fmt"
	"time"

	"github.com/go-redis/redis/v8"
)

// 歌单所需信息
type PlayList struct {
	Category string `form:"category"`
	Sort     string `form:"sort"`
	Offset   string `form:"offset"`
	Id       string `form:"id"`
	Limit    string `form:"limit"`
}

//获取歌单列表
func (service *PlayList) PlaylistList() *serializer.Response {

	if val := service.Category; val != "" {
		service.Category = val
	} else {
		service.Category = "35"
	}

	if val := service.Sort; val != "" {
		service.Sort = val
	} else {
		//5:推荐，6：最热，7：最新，8：飙升，3：热藏
		service.Sort = "5"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	url := "http://www2.kugou.kugou.com/yueku/v9/special/getSpecial?c=" + service.Category + "&t=" + service.Sort + "&p=" + service.Offset + "&is_ajax=1&cdn=cdn"

	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单信息
func (service *PlayList) PlaylistInfo() *serializer.Response {

	if val := service.Id; val != "" {
		service.Id = val
	} else {
		service.Id = "3233449"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	url := "http://api.uniquestfq.ltd/v1/kugou/playlist/info?pid=" + service.Id

	res, err := util.KugouGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单分类
func (service *PlayList) PlaylistCat() *serializer.Response {
	//定义redis key和判断redis是否有该值
	redisKey := "kugou-playlist-cat"
	redisResult, err := cache.RedisClient.Get(ctx, redisKey).Result()
	//若redis没有该值则执行获取
	if err == redis.Nil {
		url := "http://www2.kugou.kugou.com/yueku/v9/special/getSpecial?is_smarty=1"

		res, err := util.Get(url)
		if err != nil {
			return err
		}

		//将结果转为Json数据
		b, e := json.Marshal(res)
		if e != nil {
			fmt.Println(err)
		}
		//转为string存入redis
		e = cache.RedisClient.Set(ctx, redisKey, string(b), 24*time.Hour).Err()
		if e != nil {
			fmt.Println("err1", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}

		return serializer.OkResponse(res)
	} else if err != nil {
		fmt.Println("err2", err)
		return serializer.ErrorResponse(serializer.RedisError)
	} else {
		//将从redis获取的结果（string类型）转为map[string]inerface{}
		bodyMapData := map[string]interface{}{}
		err = json.Unmarshal([]byte(redisResult), &bodyMapData)
		if err != nil {
			fmt.Println("err3", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}
		return serializer.OkResponse(bodyMapData)
	}
}

//解析分享的歌单
func (service *PlayList) LocalToPlaylist() *serializer.Response {

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "30"
	}

	if val := service.Id; val != "" {
		service.Id = val
	} else {
		service.Id = "akF7bdzxV3"
	}

	url := "https://m3ws.kugou.com/zlist/list?chain=" + service.Id + "&page=" + service.Offset + "&pagesize=" + service.Limit
	res, err := util.KugouGet(url)
	if err != nil {
		fmt.Println(err)
	}

	return serializer.OkResponse(res)
}
